package com.codoon.unova.ui;

import java.util.Date;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.codoon.unova.bean.UserBean;
import com.codoon.unova.logic.ActivityManager;
import com.codoon.unova.logic.SportsDataType;
import com.codoon.unova.util.ConfigManager;
import com.codoon.unova.util.Constants;
import com.codoon.unova.util.DateUtil;
import com.codoon.unova.util.LogUtil;
import com.codoon.unova.util.NumberUtil;
import com.uwatch.helowin.R;

public class SportsMainActivity extends HorizontalBaseActivity {
	private static final int SENSOR_TYPE_STEP = Sensor.TYPE_STEP_COUNTER;
	private static final int STEP = 0, CALORIE = 1, DISTANCE = 2;
	private SensorManager mSensorManager;
	private int mLastCount = 0;// 步行总数
	protected float mDistance = 0;
	protected float mCalorie = 0;
	protected int mSteps = 0;
	
	private SensorEventListener mStepSensorLisener = new SensorEventListener() {
		@Override
		public void onSensorChanged(SensorEvent event) {
			int count = (int) event.values[0];
			if ((count - mLastCount) > 10) {
				mLastCount = count;
				Intent _intent = new Intent(Constants.ACTION_REQUEST_TODAY_STEP_CODOON);
				sendBroadcast(_intent);
			}
		}
		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sportsmain);
		currrentIndex = 0;
		ActivityManager.getInstance().pushActivity(this);
		str_date = DateUtil.DateToString(new Date());
	}

	/**
	 * Function:数据显示
	 */
	public Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			UserBean userbean = ConfigManager.getUserInfo(SportsMainActivity.this);
			mSteps = (Integer) msg.obj;
			mDistance = userbean.getStridelength() * mSteps / 1000;
			mCalorie = userbean.getWeight() * mDistance * 1.036f; //// calorie(Kcal)=体重(kg)*路程(km)*1.036;  单位千卡
			if (mDataType == SportsDataType.STEPS) {
				tv_data.setText(String.valueOf(mSteps));
				tv_unit.setText(getString(R.string.step_unit));
				circleview.updateSteps(mSteps);
			}else if (mDataType == SportsDataType.CALORIES) {
				tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mCalorie)));
				tv_unit.setText(getString(R.string.kcal_unit));
				circleview.updateSteps(mSteps);
			}else if (mDataType == SportsDataType.METERS) {
				tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mDistance)));
				tv_unit.setText(getString(R.string.km_unit));
				circleview.updateSteps(mSteps);
			}
		}
	};

	/**
	 * Function:数据切换【步数，卡路里，里程】
	 */
	protected void chagneData() {
		switch (mDataType) {
		case CALORIES:
			tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mDistance)));
			tv_unit.setText(getString(R.string.km_unit));
			mDataType = SportsDataType.METERS;
			ConfigManager.setIntValue(getApplicationContext(), ConfigManager.SPORT_MODE, DISTANCE);
			break;
		case METERS:
			tv_data.setText(String.valueOf(mSteps));
			tv_unit.setText(getString(R.string.step_unit));
			mDataType = SportsDataType.STEPS;
			ConfigManager.setIntValue(getApplicationContext(), ConfigManager.SPORT_MODE, STEP);
			break;
		case STEPS:
			tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mCalorie)));
			tv_unit.setText(getString(R.string.kcal_unit));
			mDataType = SportsDataType.CALORIES;
			ConfigManager.setIntValue(getApplicationContext(), ConfigManager.SPORT_MODE, CALORIE);
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		tv_date.setText(getString(R.string.today));

		int mode = ConfigManager.getIntValue(getApplicationContext(), ConfigManager.SPORT_MODE);  // 当前模式
		switch (mode) {
			case STEP:
				mDataType = SportsDataType.STEPS;
				tv_data.setText(String.valueOf(mSteps));
				tv_unit.setText(getString(R.string.step_unit));
				break;
			case CALORIE:
				mDataType = SportsDataType.CALORIES;
				tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mCalorie)));
				tv_unit.setText(getString(R.string.kcal_unit));
				break;
			case DISTANCE:
				mDataType = SportsDataType.METERS;
				tv_data.setText(String.valueOf(NumberUtil.decimalFloat(mDistance)));
				tv_unit.setText(getString(R.string.km_unit));
				break;
			default:
				break;
		}
		
		mLastCount = 0;
		circleview.setmSteps(mSteps);
		LogUtil.info(SportsMainActivity.class, "==onResume()===steps:" + mSteps);
		
		registerReceiver(mDataChangeReceiver, new IntentFilter(Constants.ACTION_RESPONSE_TODAY_STEP_CODOON));
		
		Intent _intent = new Intent(Constants.ACTION_REQUEST_TODAY_STEP_CODOON);
		sendBroadcast(_intent);
		
		mSensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
		mSensorManager.registerListener(mStepSensorLisener, mSensorManager.getDefaultSensor(SENSOR_TYPE_STEP), SensorManager.SENSOR_DELAY_FASTEST);
	}

	@Override
	protected void showLeft() {
		// TODO Auto-generated method stub
		Bundle bundle = new Bundle();
		bundle.putInt("currentIndex", 1);
		bundle.putString("date", str_date);
		changeView(SportsOneActivity.class, bundle, false);
		overridePendingTransition(R.anim.right_in, R.anim.right_out);
		ActivityManager.getInstance().popActivity(this);
	}
	
	@Override
	protected void onStop() {
		if (mDataChangeReceiver != null) {
			unregisterReceiver(mDataChangeReceiver);
		}
		if (mStepSensorLisener != null) {
			mSensorManager.unregisterListener(mStepSensorLisener);
		}
		super.onStop();
	}
	
	@Override
	protected void showRight() {
	}
	
	BroadcastReceiver mDataChangeReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Constants.ACTION_RESPONSE_TODAY_STEP_CODOON)) {
				int steps = intent.getIntExtra(Constants.TODAY_CODOON_STEPS, 0);
				Message msg = new Message();
				msg.what = 1;
				msg.obj = steps;
				mHandler.sendMessage(msg);
			}
		}
	};
}